package com.hd.tiyu.yuejian.Activity;


import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hd.tiyu.yuejian.Adapter.DashiketangAdapter;
import com.hd.tiyu.yuejian.Fragment.KetangFragment;
import com.hd.tiyu.yuejian.Fragment.WentiFragment;
import com.hd.tiyu.yuejian.JavaBean.Master;
import com.hd.tiyu.yuejian.Model.ListsModel;
import com.hd.tiyu.yuejian.R;
import com.squareup.picasso.Picasso;
import android.view.View.OnClickListener;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/9/30.
 */

public class MasterActivity extends FragmentActivity implements OnClickListener {

    private String master_id;
    private ImageView img;
    private Master data;
    private TextView top,introduction,province,city;
    private RecyclerView recyclerView;
    private LinearLayout Ketang;
    private LinearLayout Wenti;

    private TextView Textketang;
    private TextView Textwenti;

    private Fragment Tabketang;
    private Fragment Tabwenti;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_master);
        initView();
        initEvent();
        initData();
    }
    private void initView() {
        img=findViewById(R.id.master_imageView);
        top=findViewById(R.id.top_name);
        province=findViewById(R.id.master_province_name);
        city=findViewById(R.id.master_city_name);
        introduction=findViewById(R.id.master_introduction);
        Ketang= findViewById(R.id.tab_dashi_ketang);
        Wenti=findViewById(R.id.tab_dashi_wenti);
        Textketang=findViewById(R.id.text_ketang);
        Textwenti=findViewById(R.id.text_wenti);


        Ketang.setOnClickListener(this);
        Wenti.setOnClickListener(this);
    }
    private void initData() {
        Intent intent=getIntent();
        master_id=intent.getStringExtra("id");
        ListsModel model=new ListsModel();
        Call<Master>call=model.getDashi(master_id);
        call.enqueue(new Callback<Master>() {
            @Override
            public void onResponse(Call<Master> call, Response<Master> response) {
                data=response.body();
                Picasso.with(MasterActivity.this).load("http://172.24.10.175/workout/Uploads/"+data.getThumb()).resize(300,150).centerCrop().into(img);
                top.setText(data.getRealname());
                introduction.setText(data.getIntroduction());
                province.setText(data.getProvince_name());
                city.setText(data.getCity_name());
                setSelect(0);
            }

            @Override
            public void onFailure(Call<Master> call, Throwable t) {

            }
        });
    }

    private void initEvent() {

    }

    @TargetApi(Build.VERSION_CODES.M)
    public void setSelect(int i) {
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        Bundle bundle=new Bundle();
        bundle.putString("id",master_id);
        hideFragment(transaction);
        switch (i){
            case 0:
                if(Tabketang==null){
                    Tabketang=new KetangFragment();
                    Tabketang.setArguments(bundle);
                    transaction.add(R.id.master_content,Tabketang);
                }else {
                    transaction.show(Tabketang);
                }
                Textketang.setTextColor(getColor(R.color.lv));
                break;
            case 1:
                if(Tabwenti==null){
                    Tabwenti= new WentiFragment();
                    Tabwenti.setArguments(bundle);
                    transaction.add(R.id.master_content,Tabwenti);
                }else {
                    transaction.show(Tabwenti);
                }
                Textwenti.setTextColor(getColor(R.color.lv));
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if(Tabketang!=null)
            transaction.hide(Tabketang);
        if (Tabwenti!=null)
            transaction.hide(Tabwenti);
    }

   @TargetApi(Build.VERSION_CODES.M)
    private void resetImgs(){
        Textketang.setTextColor(getColor(R.color.bai));
        Textwenti.setTextColor(getColor(R.color.bai));
    }

    @Override
    public void onClick(View view) {
        resetImgs();
        switch (view.getId()){
            case R.id.tab_dashi_ketang:
                setSelect(0);
                break;
            case R.id.tab_dashi_wenti:
                setSelect(1);
                break;
        }
    }
}
